package org.vectrics.calendar;

import java.text.SimpleDateFormat;
import java.util.Locale;

import org.vectrics.SystemException;
import org.vectrics.calendar.struts.CalendarTag;
import org.vectrics.common.db.ExpirableRecordObject;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.user.Interview;
import org.vectrics.skill.UserEvaluation;
import org.vectrics.user.PersonCommunication;
import org.vectrics.user.PersonCommunicationMethod;
import org.vectrics.user.PersonCommunicationMethodType;
import org.vectrics.user.PersonEncounter;
import org.w3c.dom.Element;

public class RecruiterCalendarHelper {
	static final long serialVersionUID = 970932470934322534L;

	
	public void personExpirableToXml(ExpirableRecordObject expirable, Element encounterElement, Locale locale) {
		if (expirable instanceof PersonEncounter) {
			PersonEncounter encounter = (PersonEncounter)expirable;
			String uniqueId = CalendarTag.getUniqueId(expirable);  
			encounterElement.setAttribute("id", uniqueId);
			encounterElement.setAttribute("recordId", encounter.getId().toString());
			encounterElement.setAttribute("status", encounter.getEncounterStatus().getCode());
			encounterElement.setAttribute("statusDescription", encounter.getEncounterStatus().getName());
			encounterElement.setAttribute("dateStart", encounter.getDateStart().toString());
			encounterElement.setAttribute("dateEnd", encounter.getDateEnd().toString());
			SimpleDateFormat format = CoreServiceLocator.getDateFormat().getDateFormatRoundToMinute(locale);
			encounterElement.setAttribute("dateStartFriendly", format.format(encounter.getDateStart()));
			encounterElement.setAttribute("dateEndFriendly", format.format(encounter.getDateEnd()));
			encounterElement.setAttribute("toPersonName", encounter.getPerson().getName());
			encounterElement.setAttribute("fromPersonName", encounter.getLeadPerson().getName());
			encounterElement.setAttribute("class", encounter.getClass().getName());
			if (encounter instanceof PersonCommunication) {
				PersonCommunication personCommunication = (PersonCommunication)encounter;
				PersonCommunicationMethod method = personCommunication.getMethod();
				if (method.getPersonCommunicationMethodType().getCode().equals(
						PersonCommunicationMethodType.TYPE_PHONE_CALL)) {
					encounterElement.setAttribute("type", "Phone Call");
					
				} else if (method.getPersonCommunicationMethodType().getCode().equals(
						PersonCommunicationMethodType.TYPE_EMAIL)){
					encounterElement.setAttribute("type", "Email");
				} else {
					encounterElement.setAttribute("type", "Communication");
				}
			} else if (encounter instanceof Interview) {
				Interview interview = (Interview)encounter;
				encounterElement.setAttribute("type", "Interview");
				encounterElement.setAttribute("interviewStatus", interview.getStatusType().getName());
			} else if (encounter instanceof UserEvaluation) {
				UserEvaluation evaluation = (UserEvaluation)encounter;
				encounterElement.setAttribute("type", "Evaluation");
				encounterElement.setAttribute("evaluationType", evaluation.getTypeDescription());
			} else {
				encounterElement.setAttribute("type", "Encounter");
			}
		} else {
			throw new SystemException("Do not know how to handle class of type: " + expirable.getClass().getName());
		}
	}
	
	

}
